// pages/baojia/baojia.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gzJson:{},
    doorJson:{},
    wjJson:{},
    gzIdx:null,
    gzPrice: 0,
    gzLong:null,
    gzTall:null,

    doortypename: null,
    doorclname: null,
    doorcls: [],
    dooromatername: null,
    dooromaters: [],
    doormmatername: null,
    doormmaters: [],

    doorTabIdx: null,
    doorTabName: '',
    doorMateralIdx: null,
    doorMaterial:[],
    doorMaterialI:0,
    doorPrice: 0,
    doorPriceAdd: 0,
    doorbrand:[],
    mdoorbrand:[],
    mdoorMaterialI:0,
    doorLong:null,
    doorTall:null,
    wjMaterial:[],
    wjbrand: [],
    wjIdx: null,
    wjTemp: 0,
    wjPrices: [],
    totalPrice: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getJson()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getJson(){
    var _this = this
    wx.request({
      url: app.globalData.baseurl +'/product/GetCustomQuote',
      data: {
      },
      method: "GET",
      success (res) {
        var arr = res.data.Items
        arr[1].ChildCustomQuoteSets.Items = _this.filterMaterial(res.data.Items[1].ChildCustomQuoteSets.Items,'door')
        arr[2].ChildCustomQuoteSets.Items = _this.filterMaterial(res.data.Items[2].ChildCustomQuoteSets.Items,'wj')
        arr[2] = _this.addWjBrand(arr[2])
        _this.setData({
          gzJson: arr[0],
          doorJson: arr[1],
          wjJson: arr[2],
        })
        _this.setData({
          doorTabIdx: _this.data.doorJson.ChildCustomQuoteSets.Items[0].CustomQuoteSetId,
          doorTabName: _this.data.doorJson.ChildCustomQuoteSets.Items[0].CustomName,
          wjTabIdx: _this.data.wjJson.ChildCustomQuoteSets.Items[0].CustomQuoteSetId
        })
      },
      fail(res){
        console.log(res)
      }
    })
  },
  filterMaterial(list,str){
    for (let i = 0; i < list.length; i++) {
      if (list[i].IsBrand) {
          if (this.data[str+"Material"].indexOf(list[i]) == -1) {
              this.setData({
                [str+"Material"]:this.data[str+"Material"].concat(list[i])
              })
          }
          list.splice(i, 1)
          i--;
          continue;
      }
      if (list[i].ChildCustomQuoteSets.Items.length > 0) {
        this.filterMaterial(list[i].ChildCustomQuoteSets.Items,str)
      }
    }
    return list
  },
  addWjBrand(obj){
      var choose = [{label:"苔花五金",value:0}]
      if(obj.BrandIds){
      var arr = obj.BrandIds.split(",")
      arr.pop()
      arr.shift()
      if(arr.length>0){
        for(let i=0;i<this.data.wjMaterial.length;i++){
          if(arr.indexOf(this.data.wjMaterial[i].CustomQuoteSetId+'')>-1){
            choose.push({
              label:this.data.wjMaterial[i].CustomName,
              value:this.data.wjMaterial[i].Quote
            })
          }
        }
      }
    }
    if(obj.ChildCustomQuoteSets.Items.length > 0){
      for (let j = 0; j < obj.ChildCustomQuoteSets.Items.length; j++) {
        this.addWjBrand(obj.ChildCustomQuoteSets.Items[j])
      }
    }
    obj.wjbrand = choose
    obj.wjIdx = 0
    obj.num = 0
    obj.addPrice = 0
    return obj
  },
  changeValue(e){
    this.setData({
      [e.target.dataset.key]:e.detail.value
    })
    this.totalCalc()
  },
  syncLong(e){
    if(this.data.doorLong==null||this.data.doorLong==''||this.data.doorLong==0){
      this.setData({
        doorLong:e.detail.value
      })
    }
  },
  syncTall(e){
    if(this.data.doorTall==null||this.data.doorTall==''||this.data.doorTall==0){
      this.setData({
        doorTall:e.detail.value
      })
    }
  },
  changeGz(e){
    console.log(e);
    
    this.setData({
      gzIdx: e.currentTarget.dataset.key,
      gzPrice: e.currentTarget.dataset.price,
    })
    this.totalCalc()
  },

  doortypeslt(e){
    console.log(e);
    if(this.data.doortypename == this.data.doorJson.ChildCustomQuoteSets.Items[e.detail.value].CustomName){

    }else{
      this.setData({
        doorcls:this.data.doorJson.ChildCustomQuoteSets.Items[e.detail.value].ChildCustomQuoteSets.Items,
        doortypename:this.data.doorJson.ChildCustomQuoteSets.Items[e.detail.value].CustomName,
        doorclname:'',
        dooromaters:[],
        dooromatername:'',
        doormmaters:[],
        doormmatername:'',
        doorPrice:0,
        doorPriceAdd:0
      })
    }
  },
  doorclslt(e){
    console.log(e);
    console.log(this.data.doorcls[e.detail.value]);
    if(this.data.doorclname == this.data.doorcls[e.detail.value].CustomName){

    }else{
      this.setData({
        doorclname:this.data.doorcls[e.detail.value].CustomName,
        dooromaters:[],
        dooromatername:'',
        doormmaters:[],
        doormmatername:'',
        doorPriceAdd:0
      })

      if(this.data.doorcls[e.detail.value].BrandIds){
        var arr = this.data.doorcls[e.detail.value].BrandIds.split(",")
        var choose = [{label:"苔花五金",value:0}]
        arr.pop()
        arr.shift()
        if(arr.length>0){
          for(let i=0;i<this.data.doorMaterial.length;i++){
            if(arr.indexOf(this.data.doorMaterial[i].CustomQuoteSetId+'')>-1){
              choose.push({
                label:this.data.doorMaterial[i].CustomName,
                value:this.data.doorMaterial[i].Quote
              })
            }
          }
          this.setData({
            doorPrice:this.data.doorcls[e.detail.value].Quote,
            dooromaters:choose
          })
        }else{
          this.setData({
            doorPrice:this.data.doorcls[e.detail.value].Quote,
            dooromaters:[]
          })
        }
      }else{
        this.setData({
          doorPrice:this.data.doorcls[e.detail.value].Quote,
          dooromaters:[]
        })
      }
      
      if(this.data.doorcls[e.detail.value].ChildCustomQuoteSets.Items.length>0){
        var mchoose = []
        for(let i=0;i<this.data.doorcls[e.detail.value].ChildCustomQuoteSets.Items.length;i++){
            mchoose.push({
              label:this.data.doorcls[e.detail.value].ChildCustomQuoteSets.Items[i].CustomName,
              value:this.data.doorcls[e.detail.value].ChildCustomQuoteSets.Items[i].Quote
            })
        }
        this.setData({
          doorPrice:0,
          doormmaters:mchoose
        })
      }

    }


    this.totalCalc()
    
  },
  dooromaterslt(e){
    console.log(e);
    console.log(this.data.dooromaters[e.detail.value]);
    this.setData({
      doorPriceAdd:this.data.dooromaters[e.detail.value].value,
      dooromatername:this.data.dooromaters[e.detail.value].label,
    })
    this.totalCalc()
  },
  doormmaterslt(e){
    console.log(e);
    console.log(this.data.doormmaters[e.detail.value].value);
    this.setData({
      doorPrice:this.data.doormmaters[e.detail.value].value,
      doorPriceAdd:0,
      doormmatername:this.data.doormmaters[e.detail.value].label,
    })
    this.totalCalc()
  },



  changeDoor(e){
    this.setData({
      doorTabIdx: e.target.dataset.key,
      doorTabName: e.target.dataset.title,
    })
  },

  changeDoorMateral(e){
    if(e.target.dataset.key == this.data.doorMateralIdx){
      this.setData({
        doorMateralIdx:null,
        doorMaterialI:0,
        mdoorMaterialI:0,
        doorPrice: 0,
        doorPriceAdd:0,
      })
    }else{
      if(e.target.dataset.brand){
        var arr = e.target.dataset.brand.split(",")
        var choose = [{label:"苔花五金",value:0}]
        arr.pop()
        arr.shift()
        if(arr.length>0){
          for(let i=0;i<this.data.doorMaterial.length;i++){
            if(arr.indexOf(this.data.doorMaterial[i].CustomQuoteSetId+'')>-1){
              choose.push({
                label:this.data.doorMaterial[i].CustomName,
                value:this.data.doorMaterial[i].Quote
              })
            }
          }
          this.setData({
            doorPrice:e.target.dataset.price,
            doorbrand:choose
          })
        }else{
          this.setData({
            doorPrice:e.target.dataset.price,
            doorbrand:[]
          })
        }
      }else{
        this.setData({
          doorPrice:e.target.dataset.price,
          doorbrand:[]
        })
      }
      var obj = e.target.dataset.obj
      if(obj.ChildCustomQuoteSets.Items.length>0){
        var mchoose = []
        for(let i=0;i<obj.ChildCustomQuoteSets.Items.length;i++){
            mchoose.push({
              label:obj.ChildCustomQuoteSets.Items[i].CustomName,
              value:obj.ChildCustomQuoteSets.Items[i].Quote
            })
        }
        this.setData({
          doorPrice:obj.ChildCustomQuoteSets.Items[0].Quote,
          mdoorbrand:mchoose
        })
      }
      this.setData({
        doorMateralIdx:e.target.dataset.key,
        doorMaterialI:0,
        mdoorMaterialI:0,
        doorPriceAdd:0,
      })
    }
    
    this.totalCalc()

  },
  dmaterialSlt(e){
    this.setData({
      doorMaterialI:e.detail.value,
      doorPriceAdd:this.data.doorbrand[e.detail.value].value,
    })
    this.totalCalc()
  },
  mdmaterialSlt(e){
    this.setData({
      mdoorMaterialI:e.detail.value,
      doorPrice:this.data.mdoorbrand[e.detail.value].value,
      doorPriceAdd:0,
    })
    this.totalCalc()
  },
  wjmaterialSlt(e){
    var _this=this
    this.setData({
      wjJson: this.wjChangeWord(e.target.dataset.id,e.detail.value,this.data.wjJson)
    })
    _this.wjPriceCalc(e.target.dataset.id,e.target.dataset.price,this.data.wjTemp,e.target.dataset.num)
    
  },
  wjChangeWord(id,idx,obj){
    if(obj.CustomQuoteSetId == id){
      obj.wjIdx = idx
      obj.addPrice = obj.wjbrand[idx].value
      this.setData({
        wjTemp: obj.wjbrand[idx].value
      })
    }
    if(obj.ChildCustomQuoteSets.Items.length > 0){
      for (let j = 0; j < obj.ChildCustomQuoteSets.Items.length; j++) {
        this.wjChangeWord(id,idx,obj.ChildCustomQuoteSets.Items[j])
      }
    }
    return obj
  },
  changeWjNum(e){
    var _this=this
    this.setData({
      wjJson: this.wjChangeNum(e.target.dataset.id,e.detail.value,this.data.wjJson)
    })
    _this.wjPriceCalc(e.target.dataset.id,e.target.dataset.price,e.target.dataset.addprice,e.detail.value)
  },
  wjChangeNum(id,num,obj){
      if(obj.CustomQuoteSetId == id){
      obj.num = num
    }
    if(obj.ChildCustomQuoteSets.Items.length > 0){
      for (let j = 0; j < obj.ChildCustomQuoteSets.Items.length; j++) {
        this.wjChangeNum(id,num,obj.ChildCustomQuoteSets.Items[j])
      }
    }
    return obj
  },
  wjPriceCalc(id,price,addprice,num){
    var arr = this.data.wjPrices
    var _this = this
    if(num){

    }else{
      num = 0
    }
    var ishave = false
    for(let i=0;i<arr.length;i++){
      if(id == arr[i][0]){
        ishave = true
        arr[i][1] = (price + addprice) * num
      }
    }
    if(!ishave){
      arr.push([id,(price + addprice) * num])
    }
    this.setData({
      wjPrices:arr
    })
    // setTimeout(function(){
      _this.totalCalc()
    // },50)
    
  },
  totalCalc(){
    var gz = (this.data.gzPrice?this.data.gzPrice:0) * (this.data.gzLong?this.data.gzLong:0) * (this.data.gzTall?this.data.gzTall:0)
    var door = ((this.data.doorPrice?this.data.doorPrice:0) + (this.data.doorPriceAdd?this.data.doorPriceAdd:0))* (this.data.doorLong?this.data.doorLong:0) * (this.data.doorTall?this.data.doorTall:0)
    var wj = 0
    for(let i=0;i<this.data.wjPrices.length;i++){
      wj += +this.data.wjPrices[i][1]
    }
    this.setData({
      totalPrice: Math.round(gz+door+wj)
    })
  },
  reset(){
    this.setData({
      gzJson:{},
      doorJson:{},
      wjJson:{},
      gzIdx:null,
      gzPrice: 0,
      gzLong:null,
      gzTall:null,

      doortypename: null,
      doorclname: null,
      doorcls: [],
      doormatername: null,
      doormaters: [],
      dooromatername: null,
      dooromaters: [],
      doormmatername: null,
      doormmaters: [],

      doorTabIdx: null,
      doorTabName: '',
      doorMateralIdx: null,
      doorMaterial:[],
      doorMaterialI:0,
      doorPrice: 0,
      doorPriceAdd: 0,
      doorbrand:[],
      mdoorbrand:[],
      mdoorMaterialI:0,
      doorLong:null,
      doorTall:null,
      wjMaterial:[],
      wjbrand: [],
      wjIdx: null,
      wjTemp: 0,
      wjPrices: [],
      totalPrice: 0
    })
    this.onLoad()
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})